<?php

namespace App\Exceptions;

use App\Services\CommonService;
use Exception;

/**
 * 通用 api 异常
 */
class ApiErrorException extends Exception
{
    const DEFAULT_MSG = '系统繁忙！';

    protected $innerMessage = '';
    protected $code;
    protected $context = [];
    protected $data = [];
    protected $options = [];
    protected $logLevel = 'error';

    public function __construct($message = '', $innerMessage = '', $code = null, $data = [], $context = [], $options = [])
    {
        parent::__construct($message, $code);
        $code = $code ?: CommonService::HTTP_CODE_FAIL;
        $message = $message ?: self::DEFAULT_MSG;
        $this->innerMessage = $innerMessage ?: $message; // 若没有定义内部错误信息，则使用外部错误信息

        // 开发模式下显示内部错误信息
        if (config('app.debug')) {
            if ($innerMessage) {
                $message = $innerMessage;
            }
        }

        $this->code = $code;
        $this->context = $context;
        $this->data = $data;
        $this->options = $options;
        $this->logLevel = $options['logLevel'] ?? $this->logLevel;
    }



    public function getInnerMessage()
    {
        return $this->innerMessage;
    }

    public function getContext()
    {
        return $this->context;
    }

    public function getData()
    {
        return $this->data;
    }

    public function getOptions()
    {
        return $this->options;
    }

    public function getLogLevel()
    {
        return $this->logLevel;
    }
}
